﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage
    <dbPoll_Application.ViewModels.PollReportViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	SystemHistoryGraphical
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <div class="pageHeadingContainer"><h2 class="pageHeading">
        Reports</h2></div>
    <div id="contentContainer">
        <p><%= Html.ActionLink("Reports > ", "Index", "SystemReport", null,
               new { @class = "breadcrumb" })%>Session History Report</p>
        <% using (Html.BeginForm("SystemHistoryPDF","SystemReport",FormMethod.Post)) { %>
            <input type="hidden" id="image" name="image"
             value="https://chart.googleapis.com/chart?cht=map:auto=" />
            <input type="hidden" id="location" name="location" />
            <input type="submit" id="generatePDF" value="Generate PDF" /><%}%>
        <table class="reports" style="margin-bottom:20px;">
            <tr class="reports">
                <th class="reports">Poll #</th>
                <th class="reports">Poll Name</th>
                <th class="reports">Attendance</th>
                <th class="reports">Location</th>
                <th class="reports">Opening Time</th>
                <th class="reports">Closing Time</th>
                <th class="reports">Poll Masters</th>
                <th class="reports">Poll Creators</th>
            </tr>
            <%foreach (var poll in Model.allPolls)
                {%>
                    <tr class="reports">
                       <td class="reports"><%=poll.pollID%></td>
                       <td class="reports"><%=poll.name %></td>
                       <td class="reports"><%=Model.attendanceList[poll]%></td>
                       <td class="reports" id="location<%=poll.pollID%>"></td>
                       <td class="reports"><%=poll.pollStart%></td>
                       <td class="reports"><%=poll.pollClose%></td>
                       <td class="reports">
                            <%int i = 0;
                              foreach (var master in poll.pollMasters)
                              { %>
                                  <%=poll.pollMasters[i].FirstName%>
                                  <%=poll.pollMasters[i].LastName%></br>
                                  <%i++;
                              }
                            %>
                        </td>
                        <td class="reports">
                            <%foreach (var creator in Model.creatorList[poll])
                              {%>
                                  <%=creator.FirstName%> <%=creator.LastName%>
                                  </br>
                            <%}
                            %>
                        </td>
                    </tr>
              <%}
            %>
        </table>
        <div id='map_canvas'
            style="width:600px;height:350px;margin:0 auto; border:solid 0px">
            <img id="wait" src="../../Resources/possiblewait.gif"/>
            <div id="flash" style="visibility:hidden; background-color:Black;
                padding:1em 1em 1em 1em;">
                <p><font style="font-size:16px" color="white">
                <b>You need to upgrade your Adobe Flash Player to view this 
                chart.</b></font></p>
                <p align="center">
                    <a target="_blank" style="text-decoration:none"
                    href="http://get.adobe.com/flashplayer">
                    <font style="font-size:15px" color="yellow">
                    Download it from Adobe.</font>
                    </a>
                </p>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="../../Scripts/swfobject.js"></script>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type="text/javascript" 
        src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        geocoder = new google.maps.Geocoder();
        var verify=true;
        var datas=new Array();
        var countries=new Array();
        var locations = "";
        <%int y=0;
          foreach (var poll in Model.allPolls)
          {%>
            var latlng<%=y %> = new google.maps.LatLng(<%=poll.latitude%>,
                <%=poll.longitude%>);
            <%y++;
          }
        %>
        var chld="";
        var chdl="&chdl=";
        var chm="&chm=";
        function codes(callback)
        {   
            <%int x=0;
              int u=Model.allPolls.Count-1;
              foreach (var poll in Model.allPolls)
              {%> chm += "f<%=poll.name%>".replace("/ /g","+")+",000000,0,<%=x%>,10|";
                  geocoder.geocode({ 'latLng': latlng<%=x %>},
                  function(results, status)
                  {
                        if (results!=null)
                        {
                            document.getElementById('location<%=poll.pollID%>').
                                innerHTML = results[1].formatted_address;
                            locations += results[1].formatted_address +"|";
                            var place = results[1].address_components;                        
                            var c="";
                            var abc=false;
                            var bcd=false;
                            for (var i=0;i<place.length; i++)
                            {                            
                                if (place[i].types[0]=="country")
                                {
                                    countries[<%=x %>]=place[i].short_name;
                                    chld +=place[i].short_name+c+"|";
                                    if (abc==false)
                                    {
                                        datas[<%=x %>]=[place[i].long_name,
                                        <%=Model.attendanceList[poll] %>,
                                        '<%=poll.name %>'];
                                        chdl += place[i].short_name + "|";   
                                    }
                                    else {datas[<%=x %>][0]+=","+place[i].short_name;}
                                }
                                if (place[i].types[0]=="administrative_area_level_2")
                                { 
                                    if (place[i].short_name.length<4) 
                                    {
                                        c= "-" + place[i].short_name;
                                        bcd=true;
                                    }
                                    if (abc==false)
                                    {
                                        datas[<%=x %>]=[place[i].short_name,
                                        <%=Model.attendanceList[poll] %>,
                                        '<%=poll.name %>'];
                                        chdl += place[i].short_name + "|";
                                        abc=true;
                                    }
                                }
                                if (place[i].types[0]=="administrative_area_level_1" && bcd==false)
                                { 
                                    if (place[i].short_name.length<4) 
                                    {
                                        c= "-" + place[i].short_name;
                                        bcd=true;
                                    }
                                    if (abc==false)
                                    {
                                        datas[<%=x %>]=[place[i].short_name,
                                        <%=Model.attendanceList[poll] %>,
                                        '<%=poll.name %>'];
                                        chdl += place[i].short_name + "|";   
                                        abc=true;
                                    }
                                    
                                }           
                                if (place[i].types[0]=="locality")
                                {
                                    datas[<%=x %>]=[place[i].short_name,
                                        <%=Model.attendanceList[poll] %>,
                                        '<%=poll.name %>'];
                                    chdl += place[i].short_name + "|";
                                    abc = true;
                                }
                            }                            
                        }
                        else datas[<%=x %>]=null;
                     });
                   <%x++;
              }%>
              
        }
        
        var chco="&chco=0000A0|77BFC7|B3BCC0|FF0000|00FFFF|0000FF|ADA96E|E238EC|FF0080|800080|FFFF00|00FF00|FFA500|A52A2A|E56717|800000|008000|808000|FF00FF|4C7D7E|151B54|B7CEEC|342D7E|153E7E|9E7BFF|F6358A|893BFF|E67451|F778A1|C6AEC7|348781|4E9258|52D017|A0C544|E45E9D|C8B560";
        
        function drawMap(verify,countries)
        {
            var data = new google.visualization.DataTable();
            data.addRows(<%=x %>);
            data.addColumn('string', 'City');
            data.addColumn('number', 'Attendance');
            data.addColumn('string', 'Poll Name');
            <%for (int q=0; q<x; q++)
              {%>
                  if (datas[<%=q%>] !=null)
                  {
                      data.setValue(<%=q %>, 0, datas[<%=q %>][0]);
                      data.setValue(<%=q %>, 1, datas[<%=q %>][1]);
                      data.setValue(<%=q %>, 2, datas[<%=q %>][2]);
                  }
            <%}
            %>
            var options = {showZoomOut:true};
            for (var a=1;a<countries.length;a++)
            {
                if (countries[a-1]!=countries[a]) {verify=false;}
            }
            if (verify) {options['region'] = countries[0];}
            else options['region']="world";
            options['colors'] = [0xFF8747, 0xFFB581, 0xc06000];
            options['dataMode'] = 'markers';

            var container = document.getElementById('map_canvas');
            var geomap = new google.visualization.GeoMap(container);
            geomap.draw(data, options);
            google.visualization.events.addListener(geomap, 'regionClick',
            function(event)
            {
                options['region'] = event.region;
                geomap.draw(data, options);
            });
            google.visualization.events.addListener(geomap, 'zoomOut',
            function()
            {
                options['region'] = 'world'; geomap.draw(data, options);
            });
        }
        var playerVersion = swfobject.getFlashPlayerVersion();
        var output = playerVersion.major;
        if (output<9)
        {
            document.getElementById("wait").style.visibility="hidden";
            document.getElementById("flash").style.visibility="visible";
        }
        else
        {
            google.load('visualization', '1', {'packages': ['geomap']});
            var geocoder;
            onload = function() {
                var image = document.getElementById('image');
                chm=chm.slice(0,chm.length-1);
                chdl=chdl.slice(0,chdl.length-1);
                <% if (Model.allPolls.Count!=0)
                   {
                       Double max = Model.allPolls.Max(poll => poll.longitude);
                       Double min = Math.Abs(Model.allPolls.Min(poll => poll.longitude));
                       Double diff = max-min;
                       if (max-min<70) {%>var map="<%=(480-diff)/2-80%>,<%=(480-diff)/2-80%>,50,50,"<%}
                       else {%>  var map="50,50,50,50"<%}%>
                       image.value+=map+"&chs=480x280&chld="+chld+chdl+chco+chm;<%
                   }            
                   else { %>image.value =  "world.jpg"; <%}                                               
                %>
                
                locations=locations.slice(0,locations.length-1);
                document.getElementById('location').value = locations;
                if (<%=x %>==0) {
                    document.getElementById("wait").style.visibility="hidden";
                    document.getElementById('map_canvas').innerHTML=
                    "<img id=\"world\" src=\"../../Resources/world.jpg\" width=\"600\" height=\"300\" />";
                }
                else google.setOnLoadCallback(drawMap(verify,countries));                 
             }
            codes(function(callbck){});

        }
    </script>
    
</asp:Content>
